﻿using Castle.MicroKernel.SubSystems.Conversion;
using ITSM.Model;
using ITSM.Model.Entity;
using ITSM.Model.Model;
using Microsoft.Extensions.Logging;
using Org.BouncyCastle.Crypto.Tls;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ITSM.Service
{
    public class ContingencyPlanTable : BaseDB, IContingencyPlanTable
    {
        private readonly ILogger _log;
        private readonly IWxMessage _wxMessage;
        public ContingencyPlanTable(ILogger<ContingencyPlanTable> log, IWxMessage wxMessage)
        {
            _log = log;
            _wxMessage = wxMessage;
        }

        public bool AddContingencyPlanTable(RYD_ContingencyPlanTable planTable)
        {
            if (planTable.RYD_ContingencyPlanNo != 0)
            {
                return db.Updateable<RYD_ContingencyPlanTable>().Where(res => res.RYD_ContingencyPlanNo == planTable.RYD_ContingencyPlanNo).SetColumns(res => new RYD_ContingencyPlanTable
                {
                    RYD_ContingencyPlanTitle = planTable.RYD_ContingencyPlanTitle,
                    RYD_ContingencyPlanExplain = planTable.RYD_ContingencyPlanExplain,
                    RYD_ContingencyPlanDetail = planTable.RYD_ContingencyPlanDetail,
                    RYD_DefaultUsers = planTable.RYD_DefaultUsers,
                    RYD_ModifyTime = DateTime.Now,
                    RYD_ModifyBy = planTable.RYD_CreateBy
                }).ExecuteCommand() > 0;
            }

            planTable.RYD_Id = Guid.NewGuid().ToString();
            planTable.RYD_IsDelete = 1;
            planTable.RYD_CreateTime = DateTime.Now;
            return db.Insertable(planTable).ExecuteCommand() > 0;
        }

        public bool DisableContingencyPlanTable(int RYD_ContingencyPlanNo)
        {
            return db.Updateable<RYD_ContingencyPlanTable>().Where(res => res.RYD_ContingencyPlanNo == RYD_ContingencyPlanNo).ExecuteCommand() > 0;
        }

        public bool PublishContingencyPlanTable(RYD_ContingencyPlanTableDetail planTable)
        {
            planTable.RYD_Id = Guid.NewGuid().ToString();
            planTable.RYD_IsDelete = 1;
            if (db.Insertable(planTable).ExecuteCommand() > 0)
            {
                SendContingencyPlan(planTable);



                return true;
            }
            return false;
        }

        private bool SendContingencyPlan(RYD_ContingencyPlanTableDetail planTable)
        {
            try
            {
                string userArr = planTable.RYD_LeaderUsers;

                List<string> wxNos = db.Queryable<RYD_UserInfo>().Where(res => userArr.Contains(res.RYD_Id) && res.RYD_IsDelete == 1).Select(res => res.RYD_WxNo).ToList();
                string urlStart = AppHelper.ReadAppSettings("wxUrlStart");
                string urlEnd = AppHelper.ReadAppSettings("wxUrlEnd");
                foreach (var item in wxNos)
                {
                    if (planTable.RYD_CreateTime==null)
                    {
                        planTable.RYD_CreateTime = DateTime.Now;
                    }

                    M_TemplateTwo template = new()
                    {
                        client_msg_id = DateTimeOffset.Now.ToFileTime().ToString(),
                        touser = item,
                        url = urlStart + "ContingencyPlanId="+planTable.RYD_Id + urlEnd,
                        data = new M_TemplateTwo.Data()
                        {
                            first = new M_TemplateTwo.valueColor()
                            {
                                value = planTable.RYD_ContingencyPlanTitle
                            },
                            keyword1 = new M_TemplateTwo.valueColor()
                            {
                                value = planTable.RYD_CreateTime.Value.ToString("yyyy-MM-dd HH:mm")
                            },
                            keyword2 = new M_TemplateTwo.valueColor()
                            {
                                value = planTable.RYD_ContingencyPlanTitle
                            },
                            keyword3 = new M_TemplateTwo.valueColor()
                            {
                                value = planTable.RYD_ContingencyPlanExplain
                            },
                            remark = new M_TemplateTwo.valueColor()
                            {
                                value = "请注意消息！"
                            }
                        }
                    };
                    _wxMessage.SendMessage(template);
                }
            }
            catch (Exception)
            {

                throw;
            }
            return false;
        }

        public List<M_ContingencyPlanTable> QueryContingencyPlanTable(string title)
        {
            Expressionable<RYD_ContingencyPlanTable> exp = Expressionable.Create<RYD_ContingencyPlanTable>()
                      .AndIF(!string.IsNullOrEmpty(title), res => res.RYD_ContingencyPlanTitle == title)
                      .And(res => res.RYD_IsDelete == 1);
            List<RYD_ContingencyPlanTable> list = db.Queryable<RYD_ContingencyPlanTable>().Where(exp.ToExpression()).Select(res => new RYD_ContingencyPlanTable
            {
                RYD_ContingencyPlanNo = res.RYD_ContingencyPlanNo,
                RYD_ContingencyPlanTitle = res.RYD_ContingencyPlanTitle,
                RYD_ContingencyPlanExplain = res.RYD_ContingencyPlanExplain,
                RYD_CreateTime = res.RYD_CreateTime,
                RYD_DefaultUsers = res.RYD_DefaultUsers,
                RYD_CreateBy = res.RYD_CreateBy
            }).OrderBy(res => res.RYD_CreateTime, OrderByType.Desc).ToList();

            List<M_ContingencyPlanTable> listModels = new List<M_ContingencyPlanTable>();
            foreach (var item in list)
            {
                M_ContingencyPlanTable mPlanTable = new M_ContingencyPlanTable
                {
                    RYD_ContingencyPlanExplain = item.RYD_ContingencyPlanExplain,
                    RYD_ContingencyPlanNo = item.RYD_ContingencyPlanNo,
                    RYD_ContingencyPlanTitle = item.RYD_ContingencyPlanTitle,
                    RYD_CreateTime = item.RYD_CreateTime
                };
                mPlanTable.RYD_DefaultUsers = db.Queryable<RYD_UserInfo>().Where(res => item.RYD_DefaultUsers.Contains(res.RYD_Id) && res.RYD_IsDelete == 1).Select(res => new RYD_UserInfo { RYD_Id = res.RYD_Id, RealName = res.RealName, RYD_RyderNo = res.RYD_RyderNo }).ToList();

                mPlanTable.RYD_CreateBy = db.Queryable<RYD_UserInfo>().Where(res => item.RYD_CreateBy == res.RYD_Id && res.RYD_IsDelete == 1).Select(res => new RYD_UserInfo { RYD_Id = res.RYD_Id, RealName = res.RealName, RYD_RyderNo = res.RYD_RyderNo }).ToPageList(0, 1).FirstOrDefault();


                listModels.Add(mPlanTable);
            }
            return listModels;
        }

        public List<M_ContingencyPlanTable> QueryContingencyPlanTableHistory(string title)
        {
            Expressionable<RYD_ContingencyPlanTableDetail> exp = Expressionable.Create<RYD_ContingencyPlanTableDetail>()
                      .AndIF(!string.IsNullOrEmpty(title), res => res.RYD_ContingencyPlanTitle == title)
                      .And(res => res.RYD_IsDelete == 1);
            List<RYD_ContingencyPlanTableDetail> list = db.Queryable<RYD_ContingencyPlanTableDetail>().Where(exp.ToExpression()).Select(res => new RYD_ContingencyPlanTableDetail
            {
                RYD_ContingencyPlanNo = res.RYD_ContingencyPlanNo,
                RYD_ContingencyPlanTitle = res.RYD_ContingencyPlanTitle,
                RYD_ContingencyPlanExplain = res.RYD_ContingencyPlanExplain,
                RYD_CreateTime = res.RYD_CreateTime,
                RYD_LeaderUsers = res.RYD_LeaderUsers,
                RYD_CreateBy = res.RYD_CreateBy,
                RYD_Id = res.RYD_Id
            }).OrderBy(res => res.RYD_CreateTime, OrderByType.Desc).ToList();

            List<M_ContingencyPlanTable> listModels = new List<M_ContingencyPlanTable>();
            foreach (var item in list)
            {
                M_ContingencyPlanTable mPlanTable = new M_ContingencyPlanTable
                {
                    RYD_ContingencyPlanExplain = item.RYD_ContingencyPlanExplain,
                    RYD_ContingencyPlanNo = item.RYD_ContingencyPlanNo,
                    RYD_ContingencyPlanTitle = item.RYD_ContingencyPlanTitle,
                    RYD_CreateTime = item.RYD_CreateTime,
                    RYD_Id = item.RYD_Id
                };
                mPlanTable.RYD_DefaultUsers = db.Queryable<RYD_UserInfo>().Where(res => item.RYD_LeaderUsers.Contains(res.RYD_Id) && res.RYD_IsDelete == 1).Select(res => new RYD_UserInfo { RYD_Id = res.RYD_Id, RealName = res.RealName, RYD_RyderNo = res.RYD_RyderNo }).ToList();

                mPlanTable.RYD_CreateBy = db.Queryable<RYD_UserInfo>().Where(res => item.RYD_CreateBy == res.RYD_Id && res.RYD_IsDelete == 1).Select(res => new RYD_UserInfo { RYD_Id = res.RYD_Id, RealName = res.RealName, RYD_RyderNo = res.RYD_RyderNo }).ToPageList(0, 1).FirstOrDefault();


                listModels.Add(mPlanTable);
            }
            return listModels;
        }

        public RYD_ContingencyPlanTable QueryContingencyPlanTableInfo(int ContingencyPlanNo)
        {
            RYD_ContingencyPlanTable planTable = db.Queryable<RYD_ContingencyPlanTable>().Where(res => res.RYD_ContingencyPlanNo == ContingencyPlanNo && res.RYD_IsDelete == 1)
                .Select(res => new RYD_ContingencyPlanTable
                {
                    RYD_ContingencyPlanNo = res.RYD_ContingencyPlanNo,
                    RYD_ContingencyPlanTitle = res.RYD_ContingencyPlanTitle,
                    RYD_ContingencyPlanExplain = res.RYD_ContingencyPlanExplain,
                    RYD_ContingencyPlanDetail = res.RYD_ContingencyPlanDetail,
                    RYD_CreateTime = res.RYD_CreateTime,
                    RYD_DefaultUsers = res.RYD_DefaultUsers,
                    RYD_CreateBy = res.RYD_CreateBy
                }).First();
            //M_ContingencyPlanTableInfo mPlanTableInfo = new M_ContingencyPlanTableInfo
            //{
            //    RYD_ContingencyPlanExplain = planTable.RYD_ContingencyPlanExplain,
            //    RYD_ContingencyPlanNo = planTable.RYD_ContingencyPlanNo,
            //    RYD_ContingencyPlanTitle = planTable.RYD_ContingencyPlanTitle,
            //    RYD_ContingencyPlanDetail = planTable.RYD_ContingencyPlanDetail,
            //    RYD_CreateTime = planTable.RYD_CreateTime
            //};
            //mPlanTableInfo.RYD_DefaultUsers = db.Queryable<RYD_UserInfo>().Where(res => planTable.RYD_DefaultUsers.Contains(res.RYD_Id) && res.RYD_IsDelete == 1).Select(res => new RYD_UserInfo { RYD_Id = res.RYD_Id, RealName = res.RealName, RYD_RyderNo = res.RYD_RyderNo }).ToList();

            //mPlanTableInfo.RYD_CreateBy = db.Queryable<RYD_UserInfo>().Where(res => planTable.RYD_CreateBy == res.RYD_Id && res.RYD_IsDelete == 1).Select(res => new RYD_UserInfo { RYD_Id = res.RYD_Id, RealName = res.RealName, RYD_RyderNo = res.RYD_RyderNo }).ToPageList(0, 1).FirstOrDefault();

            return planTable;
        }
        public RYD_ContingencyPlanTableDetail QueryContingencyPlanTableHistoryInfo(string HistoryId)
        {
            RYD_ContingencyPlanTableDetail planTable = db.Queryable<RYD_ContingencyPlanTableDetail>().Where(res => res.RYD_Id == HistoryId && res.RYD_IsDelete == 1)
                .Select(res => new RYD_ContingencyPlanTableDetail
                {
                    RYD_ContingencyPlanNo = res.RYD_ContingencyPlanNo,
                    RYD_ContingencyPlanTitle = res.RYD_ContingencyPlanTitle,
                    RYD_ContingencyPlanExplain = res.RYD_ContingencyPlanExplain,
                    RYD_ContingencyPlanDetail = res.RYD_ContingencyPlanDetail,
                    RYD_CreateTime = res.RYD_CreateTime,
                    RYD_LeaderUsers = res.RYD_LeaderUsers,
                    RYD_CreateBy = res.RYD_CreateBy
                }).First();
            //M_ContingencyPlanTableInfo mPlanTableInfo = new M_ContingencyPlanTableInfo
            //{
            //    RYD_ContingencyPlanExplain = planTable.RYD_ContingencyPlanExplain,
            //    RYD_ContingencyPlanNo = planTable.RYD_ContingencyPlanNo,
            //    RYD_ContingencyPlanTitle = planTable.RYD_ContingencyPlanTitle,
            //    RYD_ContingencyPlanDetail = planTable.RYD_ContingencyPlanDetail,
            //    RYD_CreateTime = planTable.RYD_CreateTime
            //};
            //mPlanTableInfo.RYD_DefaultUsers = db.Queryable<RYD_UserInfo>().Where(res => planTable.RYD_DefaultUsers.Contains(res.RYD_Id) && res.RYD_IsDelete == 1).Select(res => new RYD_UserInfo { RYD_Id = res.RYD_Id, RealName = res.RealName, RYD_RyderNo = res.RYD_RyderNo }).ToList();

            //mPlanTableInfo.RYD_CreateBy = db.Queryable<RYD_UserInfo>().Where(res => planTable.RYD_CreateBy == res.RYD_Id && res.RYD_IsDelete == 1).Select(res => new RYD_UserInfo { RYD_Id = res.RYD_Id, RealName = res.RealName, RYD_RyderNo = res.RYD_RyderNo }).ToPageList(0, 1).FirstOrDefault();

            return planTable;
        }
        public M_ContingencyPlanTableInfo QueryContingencyPlanTableInfoIng(string ContingencyPlanId)
        {
            RYD_ContingencyPlanTableDetail planTable = db.Queryable<RYD_ContingencyPlanTableDetail>().Where(res => res.RYD_Id == ContingencyPlanId && res.RYD_IsDelete == 1)
                .Select(res => new RYD_ContingencyPlanTableDetail
                {
                    RYD_ContingencyPlanNo = res.RYD_ContingencyPlanNo,
                    RYD_ContingencyPlanTitle = res.RYD_ContingencyPlanTitle,
                    RYD_ContingencyPlanExplain = res.RYD_ContingencyPlanExplain,
                    RYD_ContingencyPlanDetail = res.RYD_ContingencyPlanDetail,
                    RYD_CreateTime = res.RYD_CreateTime,
                    RYD_CreateBy = res.RYD_CreateBy,
                    RYD_LeaderUsers = res.RYD_LeaderUsers
                }).First();
            M_ContingencyPlanTableInfo mPlanTableInfo = new M_ContingencyPlanTableInfo
            {
                RYD_ContingencyPlanExplain = planTable.RYD_ContingencyPlanExplain,
                RYD_ContingencyPlanNo = planTable.RYD_ContingencyPlanNo,
                RYD_ContingencyPlanTitle = planTable.RYD_ContingencyPlanTitle,
                RYD_ContingencyPlanDetail = planTable.RYD_ContingencyPlanDetail,
                RYD_CreateTime = planTable.RYD_CreateTime
            };
            mPlanTableInfo.RYD_DefaultUsers = db.Queryable<RYD_UserInfo>().Where(res => planTable.RYD_LeaderUsers.Contains(res.RYD_Id) && res.RYD_IsDelete == 1).Select(res => new RYD_UserInfo { RYD_Id = res.RYD_Id, RealName = res.RealName, RYD_RyderNo = res.RYD_RyderNo }).ToList();

            mPlanTableInfo.RYD_CreateBy = db.Queryable<RYD_UserInfo>().Where(res => planTable.RYD_CreateBy == res.RYD_Id && res.RYD_IsDelete == 1).Select(res => new RYD_UserInfo { RYD_Id = res.RYD_Id, RealName = res.RealName, RYD_RyderNo = res.RYD_RyderNo }).ToPageList(0, 1).FirstOrDefault();

            return mPlanTableInfo;
        }
    }
}
